import {
    addTopicReq,
    commentReq, deleteCommentReq,
    deleteTopicReq, deleteVodCommentReq, doGetTopicTotal,
    getCommentReq, getCommentTotalReq,
    getMyTopicMsgReq,
    getMyTopicReq,
    getMyTopicStateReq, getMyVodMsgReq, getMyVodStateReq,
    getTopicDetailReq,
    getTopicListByCommentTimeReq,
    getTopicListByJingHua,
    getTopicListByReleaseReq, reportCommentReq,
    zanCommentReq
} from "../apis/goApis";
import {handleResp} from "../utils/commonfunc";
import {Toast} from "antd-mobile";

export const topic = {
    state: {
        end: false,
        total: 0,
        state: [0, 0,],
        vodState: [0, 0,],
        topicList: {
            release: {},
            comment: {},
            jinghua: {},
        },
        topicDetail: {
            end: false,
            content: {},
            comments: {},
        },
        commentDetail: {
            end: false,
            detail: {},
            comments: {},
        },
        myZans: {
            end: false,
            lists: {},
        },
        myVodZans: {
            end: false,
            lists: {},
        },
        myComments: {
            end: false,
            lists: {},
        },
        myVodComments: {
            end: false,
            lists: {},
        },
        myTopics: {
            end: false,
            lists: {},
        },
    },

    reducers: {
        setTopList(state, payload) {

            const p = payload.p
            const list = payload.list
            const end = payload.end
            const type = payload.type

            return {
                ...state,
                topicList: {
                    ...state.topicList,
                    [type]: {
                        ...state.topicList[type],
                        end,
                        [p]: list,
                    }
                }
            }

        },

        setTopicTotal(state, payload) {
            return {
                ...state,
                total: payload.total,
            }
        },

        emptyTopicList(state, payload) {
            return {
                ...state,
                topicList: {
                    release: {},
                    comment: {},
                    jinghua: {},
                },
                end: false,
            }
        },

        emptyTopicDetail(state, payload) {
            return {
                ...state,
                topicDetail: {
                    end: false,
                    content: {},
                    comments: {},
                },
            }
        },

        emptyTopicDetailComments(state, payload) {
            return {
                ...state,
                topicDetail: {
                    ...state.topicDetail,
                    comments: [],
                }
            }
        },

        emptyCommentDetail(state, payload) {
            return {
                ...state,
                commentDetail: {
                    detail: {},
                    comments: [],
                }
            }
        },

        setTopicDetail(state, payload,) {
            return {
                ...state,
                topicDetail: {
                    ...state.topicDetail,
                    content: payload.detail,
                }
            }
        },

        setTopicDetailComments(state, payload) {
            const {
                comments,
                end,
                p,
            } = payload

            return {
                ...state,
                topicDetail: {
                    ...state.topicDetail,
                    end,
                    // comments: [...state.topicDetail.comments, ...comments,],
                    comments: {
                        ...state.topicDetail.comments,
                        [p]: comments,
                    }
                }
            }
        },

        setTopicDetailCommentsLevel2(state, payload) {
            const {
                comments,
                p,
                end,
            } = payload
            const Hfid = comments[0].Hfid
            let page = 0
            let index = 0
            const cs = {...state.topicDetail.comments}
            const entries = Object.entries(cs)
            entries.map((e, commentIndex) => {
                let commentPage = e[0]
                let comments = e[1]
                comments.map((c, commentIndex) => {
                    if (c.Id === Hfid) {
                        page = commentPage
                        index = commentIndex
                    }
                })
            })
            cs[page][index].end = end
            cs[page][index].comments.data[p] = comments
            return {
                ...state,
                topicDetail: {
                    ...state.topicDetail,
                    comments: cs,
                }
            }
        },

        setCommentDetailCommentsLevel2(state, payload) {
            const {
                comments,
                p,
                end,
            } = payload
            return {
                ...state,
                commentDetail: {
                    ...state.commentDetail,

                    // comments: [...state.commentDetail.comments, ...comments,],
                    comments: {

                        ...state.commentDetail.comments,
                        end,
                        data: {
                            ...state.commentDetail.comments.data,
                            [p]: comments,
                        }
                    },
                }
            }
        },

        setNewCommentLevel2(state, payload) {
            const newComment = payload.comment
            const comments = state.commentDetail.comments
            const detail = state.commentDetail.detail
            let newComments
            // let more = comments.length
            let more = detail.Comnum　> comments.length
            if (comments.length < 10) {
                newComments = [newComment, ...comments]
            } else {
                if (more) {
                    newComments = [newComment, ...comments]
                    const l = newComments.length
                    newComments = newComments.slice(0, l - 1)
                } else {
                    newComments = [newComment, ...comments]
                }
            }
            const newDetail = {...detail,}
            const Comnum = detail.Comnum
            newDetail.Comnum = Comnum + 1
            return {
                ...state,
                commentDetail: {
                    ...state.commentDetail,
                    detail: newDetail,
                    comments: newComments,
                }

            }
        },

        setCommentDetailLevel2(state, payload) {
            const {detail,} = payload
            // const
            return {
                ...state,
                commentDetail: {
                    ...state.commentDetail,
                    detail,
                }
            }
        },

        setNewZan(state, payload) {
            let p = 0
            let i = 0
            let entries = Object.entries(state.topicDetail.comments)
            entries.map(e => {
                const page = e[0]
                const comments = e[1]
                comments.map((c, index) => {
                    if (c.Id === payload.commentid) {
                        p = page
                        i = index
                    }
                })
            })
            const comments = {...state.topicDetail.comments}
            const {
                // Zan,
                Zannum,
            } = comments[p][i]
            comments[p][i].Zan = 1
            comments[p][i].Zannum = Zannum + 1
            return {
                ...state,
                topicDetail: {
                    ...state.topicDetail,
                    comments,
                }
            }
        },

        setMyTopicState(state, payload) {
            return {
                ...state,
                state: payload.state,
            }

        },

        setMyVodState(state, payload) {
            return {
                ...state,
                vodState: payload.state,
            }

        },

        setMyZans(state, payload) {

            const {
                p,
                list,
                end,
            } = payload

            return {
                ...state,
                myZans: {
                    ...state.myZans,
                    end,
                    lists: {
                        ...state.myZans.lists,
                        [p]: list,
                    }
                }
            }
        },

        setMyVodZans(state, payload) {
            const {
                p,
                list,
                end,
            } = payload

            return {
                ...state,
                myVodZans: {
                    ...state.myVodZans,
                    end,
                    lists: {
                        ...state.myVodZans.lists,
                        [p]: list,
                    }
                }
            }
        },

        setMyComments(state, payload) {

            const {
                p,
                list,
                end,
            } = payload


            return {
                ...state,
                myComments: {
                    ...state.myComments,
                    end: end,
                    lists: {
                        ...state.myComments.lists,
                        [p]: list,
                    }
                }
            }
        },

        setMyVodComments(state, payload) {
            const {
                p,
                list,
                end,
            } = payload


            return {
                ...state,
                myVodComments: {
                    ...state.myVodComments,
                    end: end,
                    lists: {
                        ...state.myVodComments.lists,
                        [p]: list,
                    }
                }
            }
        },

        setMyTopics(state, payload) {
            const {
                p,
                list,
                end,
            } = payload

            return {
                ...state,
                myTopics: {
                    ...state.myTopics,
                    end: end,
                    lists: {
                        ...state.myTopics.lists,
                        [p]: list,
                    }
                }
            }
        },

        doEmptyMyComments(state, payload) {
            return {
                ...state,
                myComments: {
                    end: false,
                    lists: {},
                }
            }
        },

        doEmptyMyVodComments(state, payload) {
            return {
                ...state,
                myVodComments: {
                    end: false,
                    lists: {},
                }
            }
        },


        doEmptyMyTopics(state, payload) {
            return {
                ...state,
                myTopics: {
                    end: false,
                    lists: {},
                }
            }
        },
    },

    effects: (dispatch) => ({

        doGetTopicTotal: async (payload, rootState) => {
            const resp = await doGetTopicTotal()
            handleResp(resp, dispatch, (data) => {
                const total = data.ok
                dispatch.topic.setTopicTotal({total,})
            })
        },

        doFetchTopicList: async (payload, rootState) => {
            const type = payload.type
            const p = payload.p
            const m = {
                'comment': getTopicListByCommentTimeReq,
                'release': getTopicListByReleaseReq,
                'jinghua': getTopicListByJingHua,
            }

            const resp = await m[type]({p,})
            handleResp(resp, dispatch, (data) => {
                const list = data.ok
                let end = false
                if (p === 0 && list.length < 10) {
                    end = true
                }
                if (list.length === 0) {
                    end = true
                    Toast.info('已经没有新的话题了哦')
                }
                // if (list.length < 10) {
                //     end = true
                // }
                dispatch.topic.setTopList({list, end, p, type})
            })
        },

        doAddTopic: async (payload, rootState,) => {
            const resp = await addTopicReq(payload.fm, false)
            // Toast.loading()
            handleResp(resp, dispatch, (data) => {
                if (data.ok === 1) {
                    Toast.info('发表成功')
                    if (payload.endCallback) {
                        setTimeout(() => {
                            payload.endCallback()
                        }, 1000)
                    }
                }
                if (data.err) {

                }
            })
        },

        doDeleteTopic: async (payload, rootState,) => {
            const resp = await deleteTopicReq(payload)
            handleResp(resp, dispatch, (data) => {
                const topicList = data.ok
                dispatch.topic.setTopList({topicList,})
            })
        },

        doGetTopicDetail: async (payload, rootState) => {
            const resp = await getTopicDetailReq(payload)
            handleResp(resp, dispatch, (data) => {
                const detail = data.ok
                dispatch.topic.setTopicDetail({detail,})
            })
        },

        doGetComment: async (payload, rootState) => {

            const formatComments = comments => {
                const result = []
                comments.map(c => {
                    const hfId = c.Hfid
                    if (hfId === -1) {
                        result.push(c)
                    } else {
                        const i = result.findIndex(c => c.Id === hfId)
                        if (i > -1) {
                            const cs = result[i].comments
                            if (cs) {
                                result[i].comments.data[0].push(c)
                            } else {
                                result[i].comments = {
                                    data: {0: [c,]},
                                }
                                // result[i].comments = [c,]
                            }
                        }

                    }
                })
                result.forEach(c => {
                    c.end = c.Comnum <= 10;
                })
                return result
            }

            const resp = await getCommentReq(payload)

            handleResp(resp, dispatch, (data) => {
                const p = payload.p
                const comments = formatComments(data.ok)
                let end = false
                if (comments.length < 10 && p === 0) {
                    end = true
                }
                if (comments.length === 0 && p !== 0) {
                    end = true
                    Toast.info('已经没有评论了哦')
                }
                // if (comments.length < 10) {
                //     end = true
                // }
                dispatch.topic.setTopicDetailComments({comments, end, p})
            })

        },

        doComment: async (payload, rootState) => {
            const resp = await commentReq(payload)
            return resp
        },

        doEmptyTopicList: (payload, rootState) => {
            dispatch.topic.emptyTopicList()
        },

        doEmptyTopicDetail: (payload, rootState) => {
            dispatch.topic.emptyTopicDetail()
        },

        doEmptyTopicDetailComments: (payload, rootState) => {
            dispatch.topic.emptyTopicDetailComments()
        },

        doEmptyCommentDetail: (payload, rootState) => {
            dispatch.topic.emptyCommentDetail()
        },

        doAddCommentLevel2: async (payload, rootState) => {
            const resp = await getCommentReq(payload)
            handleResp(resp, dispatch, (data) => {
                const newComment = data.ok[0]
                dispatch.topic.setNewCommentLevel2({comment: newComment,})
            })
        },

        doGetCommentLevel2: async (payload, rootState) => {
            const resp = await getCommentReq(payload)

            handleResp(resp, dispatch, (data) => {
                const comments = data.ok
                const p = payload.p
                let end = false
                if (comments.length < 10) {
                    end = true
                }
                const level = payload.level
                if (level === 1) {
                    dispatch.topic.setTopicDetailCommentsLevel2({comments, p, end})
                } else {
                    dispatch.topic.setCommentDetailCommentsLevel2({comments, p, end,})
                }

            })
        },

        doAddCommentDetailLevel2: (payload, rootState) => {
            dispatch.topic.setCommentDetailLevel2(payload)
        },

        doZanComment: async (payload, rootState) => {

            const resp = await zanCommentReq(payload)
            return resp
            // handleResp(resp, dispatch, (data) => {
            //     console.log(data)
            // })

        },

        doAddZan: (payload, rootState) => {
            dispatch.topic.setNewZan(payload)
        },

        doGetMyTopicState: async (payload, rootState) => {
            const resp = await getMyTopicStateReq(payload)
            handleResp(resp, dispatch, (data) => {
                const state = data.ok
                if (state) {
                    dispatch.topic.setMyTopicState({state,})
                }
            })
        },

        doGetMyVodState: async (payload, rootState) => {
            const resp = await getMyVodStateReq(payload)
            handleResp(resp, dispatch, (data) => {
                const state = data.ok
                if (state) {
                    dispatch.topic.setMyVodState({state,})
                }
            })
        },


        doGetMyZan: async (payload, rootState) => {
            const resp = await getMyTopicMsgReq(payload)
            handleResp(resp, dispatch, (data) => {
                const list = data.ok
                const p = payload.p
                let end = false
                if (p === 0 && list.length < 10) {
                    end = true
                }
                if (p !== 0 && list.length === 0) {
                    end = true
                }
                dispatch.topic.setMyZans({list, p, end,})
            })
        },

        doGetMyZanVod: async (payload, rootState) => {
            const resp = await getMyVodMsgReq(payload)
            handleResp(resp, dispatch, (data) => {
                const list = data.ok
                const p = payload.p
                let end = false
                if (p === 0 && list.length < 10) {
                    end = true
                }
                if (p !== 0 && list.length === 0) {
                    end = true
                }
                dispatch.topic.setMyVodZans({list, p, end,})
            })
        },


        doGetMyComments: async (payload, rootState) => {
            const resp = await getMyTopicMsgReq(payload)
            handleResp(resp, dispatch, (data) => {
                const list = data.ok
                const p = payload.p
                let end = false
                if (p === 0 && list.length < 10) {
                    end = true
                }
                if (p !== 0 && list.length === 0) {
                    end = true
                }
                dispatch.topic.setMyComments({list, p, end,})
            })
        },

        doGetMyVodComments: async (payload, rootState) => {
            const resp = await getMyVodMsgReq(payload)
            handleResp(resp, dispatch, (data) => {
                const list = data.ok
                const p = payload.p
                let end = false
                if (p === 0 && list.length < 10) {
                    end = true
                }
                if (p !== 0 && list.length === 0) {
                    end = true
                }
                dispatch.topic.setMyVodComments({list, p, end,})
            })
        },

        doGetMyTopics: async (payload, rootState) => {
            const resp = await getMyTopicReq(payload)
            handleResp(resp, dispatch, (data) => {
                const list = data.ok
                const p = payload.p
                let end = false
                if (p === 0 && list.length < 10) {
                    end = true
                }
                if (p !== 0 && list.length === 0) {
                    end = true
                }
                dispatch.topic.setMyTopics({list, p, end,})
            })
        },

        doReportComment: async (payload, rootState) => {
            const resp = await reportCommentReq(payload)
            handleResp(resp, dispatch, (data) => {
                if (data.ok) {
                    Toast.info('举报成功！')
                }

                if (data.err === 50006) {
                    Toast.info('今日举报次数已达上限')
                }
            })
        },

        doDeleteComment: async (payload, rootState) => {
            const uid = payload.uid
            const p = 0
            const resp = await deleteCommentReq(payload)
            handleResp(resp, dispatch, (data) => {
                if (data.ok === 1) {
                    dispatch.topic.doEmptyMyComments()
                    dispatch.topic.doGetMyComments({uid, p,})
                }
            })
        },

        doDeleteVodComment: async (payload, rootState) => {
            const uid = payload.uid
            const p = 0
            const resp = await deleteVodCommentReq(payload)
            handleResp(resp, dispatch, (data) => {
                if (data.ok === 1) {
                    dispatch.topic.doEmptyMyVodComments()
                    dispatch.topic.doGetMyVodComments({uid, p,})
                }
            })
        },

        doDeleteMyTopic: async (payload, rootState) => {
            const uid = payload.uid
            const p = 0
            const resp = await deleteTopicReq(payload)
            handleResp(resp, dispatch, (data) => {
                if (data.ok === 1) {
                    dispatch.topic.doEmptyMyTopics()
                    dispatch.topic.doGetMyTopics({uid, p,})
                }
            })
        },

    })
}
